﻿using System;
using System.Globalization;

namespace Syncronization.Utils
{
    /// <summary>
    /// Common guard clauses.
    /// </summary>
    internal static class Guard
    {
        #region public

        /// <summary>
        /// Checks an argument to ensure it isn't null.
        /// </summary>
        /// <param name="argumentValue">The argument value to check.</param>
        /// <param name="argumentName">The name of the argument.</param>
        public static void ArgumentNotNull(object argumentValue, string argumentName)
        {
            if (argumentValue == null)
            {
                throw new ArgumentNullException(argumentName);
            }
        }

        /// <summary>
        /// Checks a string argument to ensure it isn't null or empty.
        /// </summary>
        /// <param name="argumentValue">The argument value to check.</param>
        /// <param name="argumentName">The name of the argument.</param>
        public static void ArgumentNotNullOrEmptyString(string argumentValue, string argumentName)
        {
            ArgumentNotNull(argumentValue, argumentName);

            if (argumentValue.Trim().Length == 0)
            {
                throw new ArgumentException("The string argument is an empty string.", argumentName);
            }
        }

        /// <summary>
        /// Checks that the type is assignable.
        /// </summary>
        /// <param name="toType"></param>
        /// <param name="fromType"></param>
        public static void TypeIsAssignableFromType(Type toType, Type fromType)
        {
            if (toType.IsAssignableFrom(fromType) == false)
            {
                throw new ArgumentException(
                    string.Format(CultureInfo.CurrentCulture,
                                  "Type {0} is not assignable to type {1}.", fromType, toType));
            }
        }

        #endregion
    }
}